import Vue from 'vue'
import VueRouter from 'vue-router'
// 路由按需加载
const Login =() => import(/* webpackChunkName:"Login" */ '../components/Login.vue') 
// import Login from '../components/Login.vue'
const Home =() => import(/* webpackChunkName:"Home" */ '../components/Home.vue') 
const Welcome =() => import(/* webpackChunkName:"Home" */ '../components/Welcome.vue') 
const User =() => import(/* webpackChunkName:"User" */ '../components/user/User.vue') 
const Roles =() => import(/* webpackChunkName:"User" */ '../components/user/Roles.vue') 
const Rights =() => import(/* webpackChunkName:"User" */ '../components/user/Rights.vue') 
const Goods =() => import(/* webpackChunkName:"Goods" */ '../components/goods/Goods.vue') 
const Params =() => import(/* webpackChunkName:"Goods" */ '../components/goods/Params.vue') 
const Categories =() => import(/* webpackChunkName:"Goods" */ '../components/goods/Categories.vue') 
const AddGoods =() => import(/* webpackChunkName:"Goods" */ '../components/goods/AddGoods.vue') 
const Order =() => import(/* webpackChunkName:"Order" */ '../components/Order.vue') 
const Report =() => import(/* webpackChunkName:"Report" */ '../components/Report.vue') 

// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'
// import User from '../components/user/User.vue'
// import Roles from '../components/user/Roles.vue'
// import Rights from '../components/user/Rights.vue'
// import Goods from '../components/goods/Goods.vue'
// import Params from '../components/goods/Params.vue'
// import Categories from '../components/goods/Categories.vue'
// import AddGoods from '../components/goods/AddGoods.vue'
// import Order from '../components/Order.vue'
// import Report from '../components/Report.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    component:Login
   
  },
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:User},
      {path:'/roles',component:Roles},
      {path:'/rights',component:Rights},
      {path:'/goods',component:Goods},
      {path:'/params',component:Params},
      {path:'/categories',component:Categories},
      {path:'/goods/add',component:AddGoods},
      {path:'/orders',component:Order},
      {path:'/reports',component:Report}
    ]
  }
]

const router = new VueRouter({
  routes
})

//拦截式路由钩子事件
router.beforeEach((to,from,next)=>{
  console.log(123);
  //如果登录了，直接放行
  if(sessionStorage.getItem('token')){
    next()
  }else
  {
   
    //没登录，要访问的地址是login，直接放行
    if(to.path==='/login')
    {
      next()
    }else//不是访问login，跳转到login
    {
      next('/login')
    }
  }
})

export default router
